import { useIntersectionObserver } from '@vueuse/core'

// 定义懒加载插件
export const lazyPlugin = {
  install(app) {
    // 懒加载指令
    app.directive('img-lazy', {
      mounted(el, binding) {
        // console.log(el, binding)
        const { stop } = useIntersectionObserver(
          el,
          ([targetIsVisible]) => {
            // console.log(targetIsVisible)
            // 进入视口区
            if (targetIsVisible) {
              el.src = binding.value
              stop()
            }
          },
        )
      },
    })
  }
}
